package linuxbox.todo.taglib;

import javax.servlet.jsp.*;
import javax.servlet.jsp.tagext.*;
import javax.servlet.http.HttpServletRequest;

import linuxbox.todo.logic.BoxTodoCat;
import linuxbox.util.WebTools;
/**
 * This class list the todo categories in the SELECT tag.
 */
public class TodoCatList extends TagSupport
{
  protected boolean showSystemCat = true;
  protected String selected = null;
  protected String userid = null;

  public int doStartTag() throws JspException
  {
    try
    {
      JspWriter out = pageContext.getOut();

      out.print("<option ");
      if ( selected == null || selected.length()==0 )
        out.print(" selected ");
      out.print("></option>");

      String [] cats = null;
      //The seperator is only added when both categories are exist.
      boolean addedSeperator = true;
      if ( showSystemCat )
      {
        cats = BoxTodoCat.getSystemCategories();
        if (cats != null)
        {
          for(int i=0; i < cats.length; i++)
          {
            out.print("<option value=\""+cats[i]+"\"");
            if ( selected != null && cats[i].compareTo(selected)==0)
              out.print(" selected ");
            out.print(">"+cats[i]+"</option>");
            addedSeperator = false;
          }
        }
      }

      cats = BoxTodoCat.getUserCategories(WebTools.getBoxSession((HttpServletRequest) pageContext.getRequest()));
      if (cats != null)
      {
        for(int i=0; i < cats.length; i++)
        {
          if (!addedSeperator)
          {
          out.print("<option");
          if ( selected == null )
            out.print(" selected ");
          out.print(">  ------  </option>");
          addedSeperator = true;
        }

        out.print("<option value=\""+cats[i]+"\"");
        if ( selected != null && cats[i].compareTo(selected)==0)
          out.print(" selected ");
        out.print(">"+cats[i]+"</option>");
        }
      }
      out.flush();
    }
    catch(Exception e)
    {
      e.printStackTrace();
      throw new JspException(e.getMessage());
    }

    return SKIP_BODY;
  }

  public void setshowsystemcat(String aValue)
  {
    showSystemCat = new Boolean(aValue).booleanValue();
  }

  public void setUserid(String aValue)
  {
    userid = aValue;
  }

  public void setSelected(String aValue)
  {
    selected = aValue;
  }
}
